const app = getApp();
var api = require('../../api.js');
var mta = require('../../mta_analysis.js');

Page({
  data: {
    list: [],
    page:0
  },
  onLoad: function (options) {
    var page = this ;
    app.bind();
    page.getRecommend();

    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        page.setData({
          networkType: res.networkType
        })
      }
    });

    mta.Page.init();
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
  
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    app.bind();
    var page = this;

    page.getRecommend();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 显示加载图标  
    wx.showLoading({
      title: '玩命加载中',
    });

    if (that.data.page < that.data.count) {
      that.setData({
        page: that.data.page + 1
      });
    }

    that.getRecommend();
    // 隐藏加载框  
    wx.hideLoading();
  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    console.log(url + "?user_id=" + user_id);
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };  
  },
  getRecommend: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    var url = api.three.recommend;
    console.log(access_token);
    console.log(url);
    wx.request({
      url:api.three.recommend,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        page:that.data.page
      },
      success: function (e) {
        console.log(e);
        console.log(e.data.data.list);
        if (e.data.code == 0) {
          that.setData({
            list: e.data.data.list,
            row_count: e.data.data.row_count
          })  
          console.log(that.data.row_count);
          Math.ceil(that.data.row_count / 10);
          that.setData({
            count: Math.ceil(that.data.row_count / 10)
          })
          console.log(that.data.count)        
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }

      }
    });
  },
  toRecommend: function (e) {
    // console.log(this.data.list);
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var list = this.data.list;
    // let str = JSON.stringify(list[index]);
    var section_id = list[index].id;
    var lesson_id = list[index].lesson_id;
    // console.log(list);
    if (list[index].is_buy == true || list[index].is_buy == 1) {
      wx.navigateTo({
        url: '/pages/classDetail/classDetail?section_id=' + section_id + '&lesson_id=' + lesson_id,
      })
    } else {
      if (list[index].is_free == 1) {
        wx.navigateTo({
          url: '/pages/freeDetail/freeDetail?section_id=' + section_id + '&lesson_id=' + lesson_id,
        })
      } else {

        wx.navigateTo({
          url: '../../pages/classColumn/classColumn?lessonId=' + id,
        })
        // wx.showModal({
        //   title: '提示',
        //   content: '请购买本专栏',
        //   cancelText: '暂不购买',
        //   confirmText: '立即购买',
        //   success: function(res) {
        //     if (res.confirm) {
        //       console.log('用户点击确定');
              
        //     } else if (res.cancel) {
        //       console.log('用户点击取消')
        //     }
        //   }
        // });
      }
    }
  },

  //跳转到首页
  toIndex: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  }
})